/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.*;
import com.thinkgem.jeesite.modules.sys.service.*;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 店铺Controller
 *
 * @version 2016-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tbShopController")
public class TbShopController extends BaseController {

	@Autowired
	private TbAgentProductService tbAgentProductService;
    @Autowired
    private TbShopProductService tbShopProductService;

    @Autowired
    private UserService userService;

	@Autowired
	private TbShopService tbShopService;

    @Autowired
    private TbProductService tbProductService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbOrderProductService tbOrderProductService;

	/**
	 *展示所属代理商下的所有商品
	 */
	@RequestMapping(value = "showAgentProductList")
	public String showAgentProductList(TbAgentProduct tbAgentProduct, HttpServletRequest request, HttpServletResponse response, Model model){
		String uid = UserUtils.getUser().getId();
        User user=userService.queryUser(uid);
        tbAgentProduct.setAgentId(user.getAgentId());
        model.addAttribute("name",tbAgentProduct.getTitle());
        //如果是第一次进来，则商品类型为空，默认为便利生活
        if(null==tbAgentProduct.getReserveFlag()||"".equals(tbAgentProduct.getReserveFlag())){
            tbAgentProduct.setReserveFlag("4");
        }
        TbShop tbShop=tbShopService.selectByUidAndStatus(uid);
        if(tbShop!=null){
            Page<TbAgentProduct> page=tbAgentProductService.findList(new Page<TbAgentProduct>(request,response),tbAgentProduct);
            //判断是否是预定商品
            if(tbAgentProduct.getReserveFlag().equals("3")){
                for (int i=0;i<page.getList().size();i++){
                    TbAgentProduct tt=page.getList().get(i);
                    //如果是预定商品，并且不是每天预定的商品
                    if(tt.getGender().equals("0")){
                        Date date=new Date();
                        //如果当前时间大于不是每天预定的商品的结束时间，则不显示在店铺的预定商品中
                        if(date.compareTo(tt.getReserveEndTime())>=0){
                            page.getList().remove(i);
                            i--;
                        }
                    }
                }
            }
            model.addAttribute("page",page);
            if(page.getList().size()==0){
                model.addAttribute("message1","无查询结果");
            }
        }else{
            model.addAttribute("page",new Page<TbAgentProduct>(request,response));
            model.addAttribute("message","当前店铺未开业，请联系代理商帮您开业，开业后可以进货！");
        }
        model.addAttribute("tbProduct",tbAgentProduct);
		return "modules/sys/showAgentProductList";
	}
    /**
     *展示所属代理商下的预定
     */
    @RequestMapping(value = "showAgentProduct/{reserveFlag}")
    public String showAgentProduct(TbProduct tbProductt, @PathVariable("reserveFlag") Integer reserveFlag, HttpServletRequest request, HttpServletResponse response, Model model){
        String uid = UserUtils.getUser().getId();
        User user=userService.queryUser(uid);
        tbProductt.setAgentId(user.getAgentId());
        TbShop tbShop=tbShopService.selectByUidAndStatus(uid);
        tbProductt.setReserveFlag(reserveFlag+"");
        if(tbShop!=null&&tbShop.getId()!=null){
            tbProductt.setShopId(tbShop.getId()+"");
            //代理商上架的预定商品
            Page<TbProduct> page=tbAgentProductService.findAgentReserveProductList(new Page<TbProduct>(request,response),tbProductt);
            List<TbShopProduct> list = tbShopProductService.findAllProByShopId(tbProductt.getShopId());//店铺已入库预定商品

            List<TbProduct> listPro = page.getList();
            for(TbProduct tbProduct2:listPro){
                tbProduct2.setFlag(0);
                for(TbShopProduct tbShopProduct:list){
                    //如果代理商上架的预定商品和店铺已有的预定商品id相同，属性相同，则显示为已入库
                    if(tbProduct2.getProId().equals(tbShopProduct.getProductId())&&tbProduct2.getAttr().equals(tbShopProduct.getColor())){
                        tbProduct2.setFlag(1);
                        tbProduct2.setReserveStartTime01(tbShopProduct.getReserveStartTime01());
                        tbProduct2.setReserveEndTime01(tbShopProduct.getReserveEndTime01());
                        tbProduct2.setUpdateTimeStr(DateUtils.formatDateTime(tbShopProduct.getUpdateTime()));
                            break;
                    }tbProduct2.setUpdateTimeStr(DateUtils.formatDateTime(tbShopProduct.getUpdateTime()));
                }
            }
            model.addAttribute("list",list);
            model.addAttribute("page",page);
        }else{
            model.addAttribute("page",new Page<TbProduct>(request,response));
            model.addAttribute("message","当前店铺未开业，请联系代理商帮您开业，开业后可以进货！");
        }
        model.addAttribute("tbProductt",tbProductt);
//        if(StringUtils.isEmpty(message)){
//            model.addAttribute(message);
//        }
        return "modules/sys/showReserveProduct";
    }
	/**
	 * 商家我要进货提交订单
	 */
	@RequestMapping(value="buyAgentProduct")
    @ResponseBody
	public TbOrder buyAgentProduct(String str,HttpServletRequest request,Model model){
        TbOrder order = tbOrderService.insertOrder(request,str);
        model.addAttribute("message", "提交订单成功");
        return order;
	}
    @RequestMapping(value = "saoma")
    public String saoma(HttpServletRequest request,Model model){
        String url = request.getParameter("urlApply");
        String orderId = request.getParameter("orderId");
        String type = request.getParameter("type");//1 一键下单 区分便利生活
        String chanel=request.getParameter("productType");
        TbOrder order = tbOrderService.selectOrderById(orderId);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(url);
        stringBuffer.append("%26orderId=");
        stringBuffer.append(""+orderId+"");
        model.addAttribute("url",stringBuffer.toString());
        model.addAttribute("orderId",orderId);
        model.addAttribute("money",order.getPayAmount());
        model.addAttribute("type",type);
        model.addAttribute("channel",chanel);
        return "modules/sys/order";
    }
    @RequestMapping(value = "toapply")
    public String toApply(HttpServletRequest request,Model model){
        String orderId = request.getParameter("orderId");
        String chanel=request.getParameter("productType");
        TbOrder order = tbOrderService.selectOrderById(orderId);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(order.getOrderUrl());
        stringBuffer.append("%26orderId=");
        stringBuffer.append(""+orderId+"");
        model.addAttribute("url",stringBuffer.toString());
        model.addAttribute("orderId",orderId);
        model.addAttribute("money",order.getPayAmount());
        model.addAttribute("channel",chanel);
        return "modules/sys/order";
    }
    @ResponseBody
    @RequestMapping(value = "checkProductNum")
    public String checkProductNum(Integer id, Integer shopnum) {
        //根据id得到库存，并判断与页面输入值的大小
        boolean isok=tbAgentProductService.checkNum(id,shopnum);
        if (isok){
            return "1";
        } else {
            return "2";
        }

    }

    /**
     * 預定生活 訂單商品列表  篩選代理商后 提供一鍵下單
     * 顯示訂單内商品的 名稱 數量 價格 代理商
     * 代理商表 商品表 訂單表 訂單明細表
     * @return
     */
    @RequestMapping(value = "reverseProductOrderList")
    public String reverseProductOrderList(ReverseProduct reverseProduct,HttpServletRequest request,HttpServletResponse response,Model model) {
        if(null!=reverseProduct.getBeginDate() && null!=reverseProduct.getEndDate()){
            String beginDate = DateUtils.formatDate(reverseProduct.getBeginDate())+"00:00:00";
            String endDate = DateUtils.formatDate(reverseProduct.getBeginDate())+"23:59:59";
            reverseProduct.setBeginDate(DateUtils.parseDate(beginDate));
            reverseProduct.setEndDate(DateUtils.parseDate(endDate));
        }
        String uid = UserUtils.getUser().getId();
        User user=userService.queryUser(uid);
        TbShop tbShop=tbShopService.selectByUidAndStatus(uid);
        reverseProduct.setAgentId(UserUtils.getUser().getAgentId());
        reverseProduct.setChannelId("3");
        if(null!=tbShop){
            reverseProduct.setShopId(tbShop.getId());
            Page<ReverseProduct> page = tbShopProductService.selectReverseProductList(new Page<ReverseProduct>(request, response),reverseProduct);
            model.addAttribute("page",page);
            model.addAttribute(reverseProduct);
        }else{
            Page<ReverseProduct> page =new Page<ReverseProduct>();
            model.addAttribute("page",page);
            model.addAttribute(reverseProduct);
            model.addAttribute("message1","请到店铺中心的店铺信息中维护店铺所在小区！");
        }

        return  "modules/sys/reverseProductOrderList";
    }
    @RequestMapping(value = "reverseProductOrderListForSea")
    public String reverseProductOrderListForSea(ReverseProduct reverseProduct,HttpServletRequest request,HttpServletResponse response,Model model) {
        if(null!=reverseProduct.getBeginDate() && null!=reverseProduct.getEndDate()){
            String beginDate = DateUtils.formatDate(reverseProduct.getBeginDate())+"00:00:00";
            String endDate = DateUtils.formatDate(reverseProduct.getBeginDate())+"23:59:59";
            reverseProduct.setBeginDate(DateUtils.parseDate(beginDate));
            reverseProduct.setEndDate(DateUtils.parseDate(endDate));
        }
        String uid = UserUtils.getUser().getId();
        User user=userService.queryUser(uid);
        TbShop tbShop=tbShopService.selectByUidAndStatus(uid);
        reverseProduct.setAgentId(UserUtils.getUser().getAgentId());
        reverseProduct.setChannelId("2");
        if(null!=tbShop){
            reverseProduct.setShopId(tbShop.getId());
            Page<ReverseProduct> page = tbShopProductService.selectReverseProductList(new Page<ReverseProduct>(request, response),reverseProduct);
            model.addAttribute("page",page);
            model.addAttribute(reverseProduct);
        }else{
            Page<ReverseProduct> page =new Page<ReverseProduct>();
            model.addAttribute("page",page);
            model.addAttribute(reverseProduct);
            model.addAttribute("message1","请到店铺中心的店铺信息中维护店铺所在小区！");
        }

        return  "modules/sys/reverseProductOrderListForSea";
    }
}
